//
//


//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHAnchorViewController.h"
#import "LJHCollectionCell.h"
#import "LJHHeaderView.h"
#import "LJHListModel.h"
#import "MJRefresh.h"
#import "LJHAppTools.h"
#import "LJHAnchorListModel.h"
#import "LJHAnchorThreeModel.h"
#import "LJHAuthorDetailViewController.h"

@interface LJHAnchorViewController ()<UITableViewDelegate,UITableViewDataSource,PicTableCellDelegate>

@property(nonatomic,retain)LJHBaseTableView *anchorTableView;
@property(nonatomic,retain)NSMutableArray *anListArr;
@property(nonatomic,retain)NSMutableArray *anThreeArr;

@end

@implementation LJHAnchorViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.anListArr = [NSMutableArray array];
    self.anThreeArr = [NSMutableArray array];
    
    self.anchorTableView = [[LJHBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-150)];
    self.anchorTableView.delegate = self;
    self.anchorTableView.dataSource = self;
    [self.anchorTableView registerClass:[LJHCollectionCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.anchorTableView];
    [self downTableView];
    
    self.nextPage = 1;

    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downTableView)];
    self.anchorTableView.footer = footer;
    

}

#pragma mark - 上拉加载

-(void)downTableView
{
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_index?device=android&page=%ld",self.nextPage];
    
    [LJHAppTools afnGetRequest:url_string block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *arrAnList = [dic objectForKey:@"list"];

        
        for (NSDictionary *dicAn in arrAnList) {
            LJHAnchorListModel *anListModel = [LJHAnchorListModel modelWithDictionary:dicAn];
            [self.anListArr addObject:anListModel];
            [self.anThreeArr addObject:anListModel.list];
        }
        
        NSLog(@"%lu",(unsigned long)self.anListArr.count);

        self.nextPage++;
        [self.anchorTableView.footer endRefreshing];
        [self.anchorTableView reloadData];
                
    } error:^(NSError *error) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
}

#pragma mark - tableView协议方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.anListArr.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return WIDTH/2.5;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LJHCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (self.anThreeArr.count!=0) {
        
    cell.picTableCellDelegate = self;
    cell.anAllPicArray = [self.anThreeArr objectAtIndex:indexPath.section];
        
    }

     return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LJHHeaderView *headView = [[LJHHeaderView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    if (self.anListArr.count!=0) {
    headView.cateLable.text = ((LJHAnchorListModel *)[self.anListArr objectAtIndex:section]).title;
    }
    headView.imageMore.alpha = 0;
    
    return headView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 30;
    
}

#pragma mark - 协议点击跳转方法

-(void)JumpToAlbumMin:(NSString *)myTitle :(NSString *)trackId
{
    
    LJHAuthorDetailViewController *authorDetailVC = [[LJHAuthorDetailViewController alloc] init];
    
    authorDetailVC.uid = trackId.integerValue;
    authorDetailVC.myTitle = myTitle;
    
    authorDetailVC.navigationController.navigationBar.translucent = YES;
    [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:authorDetailVC];
    [self.view.window.rootViewController presentViewController:authorDetailVC animated:YES completion:^{
        
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
